Migo商城2.0 商品规格参数的实现(2) 十一

Migo商城2.0 商品规格参数的实现(2) 十一

接上篇

初始化类目选择组件里的回调函数的执行:

点击提交的js代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$("#itemParamAddTable .submit").click(function(){
//这里就是拼接出下面的数据格式的代码,然后stringfy序列化json字符串
var params = [];
var groups = $("#itemParamAddTable [name=group]");
groups.each(function(i,e){
var p = $(e).parentsUntil("ul").parent().find("[name=param]");
var _ps = [];
p.each(function(_i,_e){
var _val = $(_e).siblings("input").val();
if($.trim(_val).length>0){
_ps.push(_val);
}
});
var _val = $(e).siblings("input").val();
if($.trim(_val).length>0 && _ps.length > 0){
params.push({
"group":_val,
"params":_ps
});
}
});
var url = "/rest/item/param/"+$("#itemParamAddTable [name=cid]").val();
//JSON.stringify将js的对象序列化为json字符串,之前有人见有人犯过这样的小错误
$.post(url,{"paramData":JSON.stringify(params)},function(data){
$.messager.alert('提示','新增商品规格成功!',undefined,function(){
$(".panel-tool-close").click();
$("#itemParamList").datagrid("reload");
});
});
});
});

提交的数据格式为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[
{
"group": "主体",
"params": [
"品牌\t",
"类型",
"适用型号"
]
},
{
"group": "规格",
"params": [
"容量",
"速度级别",
"读写速度\t"
]
}
]

后台实现:

提交的URL:/rest/item/param/{cid}

请求参数:Long cid String paramData

代码实现:

controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 新增规格参数模板
*/
@RequestMapping(value = "{itemCatId}",method = RequestMethod.POST)
public ResponseEntity<Void> addItemParamByItemCatId(
@PathVariable("itemCatId") Long itemCatId,@RequestParam("paramData") String paramData
){
try {
if (logger.isInfoEnabled()) {
logger.info("新增某商品规格模板参数模板 itemCatId= {},paramData = {}",itemCatId,paramData);
}
ItemParam itemparam=new ItemParam();
itemparam.setId(null); //置空,防止恶意修改 `id` bigint(20) NOT NULL AUTO_INCREMENT
itemparam.setItemCatId(itemCatId);
itemparam.setParamData(paramData);
this.itemParmService.save(itemparam);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
logger.error("新增某商品规格模板参数模板 失败 itemCatId= {},"+itemCatId+"paramData = {}"+paramData,e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}

打算添加个对讲机的,谁知道jd太水,没有这个规格参数

添加个遥控车的参数

运行项目,测试结果如图:

添加的数据在数据库里的格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[
{
"group": "主体",
"params": [
"品牌",
"车型",
"包装\t"
]
},
{
"group": "电源",
"params": [
"充电\t",
"无线遥控",
"有线遥控\t"
]
},
{
"group": "尺寸",
"params": [
"产品净尺寸(长*宽*高",
"带包装尺寸(长*宽*高"
]
}
]

查询规格参数模板列表

请求URL:/rest/item/param/list

因返回的数据格式涉及到两张表 tb_item_param tb_item_cat 所以不能使用通用mapper

新建pojo ItemParamexd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.migo.pojo;

import java.util.Date;

/**
* Author 知秋
* Created by kauw on 2016/10/6.
*/
public class ItemParamexd {
private Long id;

private Long itemCatId;

private Date created;

private Date updated;

private String paramData;

private String itemCatName;


public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getItemCatId() {
return itemCatId;
}

public void setItemCatId(Long itemCatId) {
this.itemCatId = itemCatId;
}

public String getItemCatName() {
return itemCatName;
}

public void setItemCatName(String itemCatName) {
this.itemCatName = itemCatName;
}

public String getParamData() {
return paramData;
}

public void setParamData(String paramData) {
this.paramData = paramData;
}

public Date getUpdated() {
return updated;
}

public void setUpdated(Date updated) {
this.updated = updated;
}
}

新建mapper ItemparamexdMapper

这里就直接使用注解查询了(参照第一版的xml配置,可做对比)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.migo.mapper;

import com.migo.pojo.ItemParamexd;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
* Author 知秋
* Created by kauw on 2016/10/6.
*/
public interface ItemparamexdMapper {

@Select(" select\n" +
" a.id id ,a.item_cat_id itemCatId,a.created created,a.updated updated,a.param_data paramData,b.`name` itemCatName\n" +
" FROM\n" +
" tb_item_param AS a LEFT JOIN tb_item_cat as b\n" +
" ON\n" +
" a.item_cat_id=b.id ")
List<ItemParamexd> selectList();
}

service功能实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.migo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.migo.mapper.ItemparamexdMapper;
import com.migo.pojo.EasyUIDataGridResult;
import com.migo.pojo.ItemParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Author 知秋
* Created by kauw on 2016/11/14.
*/
@Service
public class ItemParmService extends BaseService<ItemParam> {
@Autowired
private ItemparamexdMapper itemparamexdMapper;

public EasyUIDataGridResult getItemParamList(int page, int rows) {
//分页处理
PageHelper.startPage(page, rows);
//执行查询

List list = itemparamexdMapper.selectList();
//取分页信息
PageInfo pageInfo = new PageInfo<>(list);
//返回处理结果
EasyUIDataGridResult result = new EasyUIDataGridResult();
result.setTotal(pageInfo.getTotal());
result.setRows(pageInfo.getList());
return result;
}
}

controller实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 查询规格参数模板列表
*/
@RequestMapping(value = "/list",method = RequestMethod.GET)
public ResponseEntity<EasyUIDataGridResult> getItemparmList(Integer page,Integer rows){
try {
if (logger.isInfoEnabled()) {
logger.info("查询商品规格模板参数模板 page= {},rows = {}",page,rows);
}
EasyUIDataGridResult result=this.itemParmService.getItemParamList(page,rows);
return ResponseEntity.ok(result);
} catch (Exception e) {
logger.error("查询商品规格参数模板列表出错:page="+page+",rows="+rows,e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}

启动项目,运行查看:

因删除比较简单,这里就不做了

您的支持将鼓励我继续创作!